home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 04 - 1988 / 04.02 Feb 88 / pascal sources / Plot Project Stuff / plotter main < prev    next >
Encoding:
Text File  |  1988-01-08  |  10.4 KB  |  384 lines  |  [TEXT/PJMM]

  1. { Quadratic Equation Example program    }
  2. { By Dave Kelly & Dave Smith             }
  3. { ©MacTutor, 1988                        }
  4.  
  5. PROGRAM QuadraticPlotter;
  6.  
  7.     USES
  8.         ROM85, PrintTraps, PlotGlobals, Misc, myPlotStuff;
  9.  
  10.     PROCEDURE crash;
  11.     BEGIN
  12.         ExitToShell;
  13.     END;
  14.  
  15.     PROCEDURE InitMac;
  16.         VAR
  17.             err : OSErr;
  18.     BEGIN
  19.         MaxApplZone;
  20.         MoreMasters;
  21.         MoreMasters;
  22.         MoreMasters;
  23.         MoreMasters;
  24.         MoreMasters;
  25.         InitGraf(@thePort);
  26.         InitFonts;
  27.         InitWindows;
  28.         InitMenus;
  29.         TEInit;
  30.         InitDialogs(@crash);
  31.         InitCursor;
  32.         FlushEvents(everyEvent, 0);
  33.         Finished := false;
  34.         dialogflg := false;
  35.  
  36.         color[1] := 33;    {black}
  37.         color[2] := 30;    {white}
  38.         color[3] := 205;    {red}
  39.         color[4] := 341;    {green}
  40.         color[5] := 409;    {blue}
  41.         color[6] := 273;    {cyan}
  42.         color[7] := 137;    {magenta}
  43.         color[8] := 69;    {yellow}
  44.  
  45.         theWorld.machineType := 4;        {Mac II}
  46.     {err := SysEnvirons(1, theWorld);     SysEnvirons not in LSP 1.11}
  47.         typeOfMac := theWorld.machineType;
  48.  
  49.         IF (typeOfMac >= 0) AND (NGetTrapAddress(WNETrapNum, ToolTrap) = NGetTrapAddress(UnImplTrapNum, ToolTrap)) THEN
  50.             BEGIN
  51.                 WNE := FALSE;
  52.                 doMessage('WaitNextEvent not implemented', '', '', ''); {Only true under Multifinder}
  53.             END
  54.         ELSE
  55.             WNE := TRUE;
  56.         mouseRgn := NewRgn;
  57.     END;
  58.  
  59.     PROCEDURE initRects;
  60.         VAR
  61.             MemoryPtr : ^Integer;
  62.     BEGIN
  63.         MemoryPtr := pointer(mBarHeightGlobal); {System Global mBarHeight}
  64.         mBarHeight := MemoryPtr^;
  65.         screen := ScreenBits.Bounds; {current screen device}
  66.         SetRect(DragArea, Screen.left + 4, Screen.top + mBarHeight + 4, Screen.right - 4, Screen.bottom - 4);
  67.         SetRect(GrowArea, Screen.left + MinWidth, Screen.top + MinHeight, Screen.right - 8, Screen.bottom - 8);
  68.         SetRect(PlotWindowRect, Screen.left + 15, Screen.top + 45, Screen.right - 75, Screen.bottom - 50);
  69.         SetRect(ZoomRect, Screen.left + 4, Screen.top + mBarHeight + 4, Screen.right - 4, Screen.bottom - 4);
  70.     END;
  71.  
  72.     PROCEDURE InitMyWindow;
  73.         VAR
  74.             windtype : integer;
  75.             Visible : boolean;
  76.             GoAway : boolean;
  77.             RefVal : LongInt;
  78.             title : str255;
  79.             myPrint : THPrint;
  80.     BEGIN
  81.         Visible := false;
  82.         windtype := documentProc + ZoomBox;
  83.         GoAway := true;
  84.         RefVal := 0;
  85.         title := 'Quadratic Plotter';
  86.         PlotWindow := NewWindow(NIL, PlotWindowRect, title, Visible, windtype, pointer(-1), GoAway, RefVal);
  87.         PlotWindowPeek := WindowPeek(PlotWindow);
  88.         SetPort(PlotWindow);
  89.         TextFont(Geneva);
  90.         TextSize(10);
  91.         TextFace([]);    {plain}
  92.         TextMode(1);    {Or}
  93.         PenNormal;
  94.         ForeColor(blackColor);
  95.         BackColor(whiteColor);
  96.         PlotDocHandle := DocHandle(NewHandle(sizeof(Document)));
  97.         PlotWindowPeek^.refCon := LongInt(PlotDocHandle);
  98.         PlotWindowPeek^.windowKind := userKind;
  99.         PlotDocHandle^^.drawing := NIL;
  100.         WITH PlotWindow^.portRect DO
  101.             BEGIN
  102.                 SetRect(VCRect, right - (SBarWidth - 1), top - 1, right + 1, bottom - (SBarWidth - 2));
  103.                 SetRect(HCRect, left - 1, bottom - (SBarWidth - 1), right - (SBarWidth - 2), bottom + 1);
  104.                 SetRect(GrowRect, HCRect.right, HCRect.top, VCRect.right, HCRect.bottom);
  105.                 SetRect(PicRect, left, top, right - (SBarWidth - 1), bottom - (SBarWidth - 1));
  106.                 SetRect(PageRect, left, top, right - (SBarWidth - 1), bottom - (SBarWidth - 1));
  107.             END;  {of with }
  108.         ClipRect(PlotWindow^.portRect);
  109.         myPrint := THPrint(NewHandle(SIZEOF(TPrint)));
  110.         PlotDocHandle^^.Print := myPrint;
  111.     END;
  112.  
  113.     PROCEDURE InitMyMenus;
  114.         VAR
  115.             i : integer;
  116.     BEGIN
  117.         myMenus[AppleM] := GetMenu(AppleMenu);
  118.         AddResMenu(myMenus[AppleM], 'DRVR');
  119.         myMenus[FileM] := GetMenu(FileMenu);
  120.         myMenus[EditM] := GetMenu(EditMenu);
  121.         myMenus[ColorM] := GetMenu(ColorMenu);
  122.         myMenus[OptionM] := GetMenu(OptionMenu);
  123.         myMenus[GraphM] := GetMenu(GraphMenu);
  124.         myMenus[AxisM] := GetMenu(AxisMenu);
  125.         myMenus[BackgroundM] := GetMenu(BackgroundMenu);
  126.  
  127.         FOR i := 1 TO MenuCount DO
  128.             InsertMenu(myMenus[i], 0);
  129.         FOR i := SubMenuStart TO TotalMenuCount DO
  130.             InsertMenu(myMenus[i], -1);
  131.  
  132.         GraphColor := 3;
  133.         CheckItem(myMenus[GraphM], GraphColor, true);
  134.         AxisColor := 1;
  135.         CheckItem(myMenus[AxisM], AxisColor, true);
  136.         BackgroundColor := 2;
  137.         CheckItem(myMenus[BackgroundM], BackgroundColor, true);
  138.  
  139.         CheckItem(myMenus[OptionM], 1, true);
  140.         Option := 1;  {window rect printing}
  141.  
  142.         DrawMenuBar;
  143.     END;    {of proc}
  144.  
  145.     PROCEDURE doMouse (myEvent : EventRecord);
  146.         VAR
  147.             whereIsIt : integer;
  148.             whichWindow : WindowPtr;
  149.             localPt, globalPt : Point;
  150.             oldPort : GrafPtr;
  151.     BEGIN
  152.         globalPt := myEvent.where;
  153.         localPt := globalPt;        {global coord of mouse}
  154.         GlobalToLocal(localPt);     {local coord of mouse}
  155.         whereIsIt := FindWindow(globalPt, whichWindow);
  156.         CASE whereIsIt OF
  157.             inDesk :             {0}
  158.                 doMessage('Mouse Click on Desktop.', '(Not handled in this program.)', '', '');
  159.             inMenuBar :      {1}
  160.                 doMenuBar(MenuSelect(globalPt));
  161.             inSysWindow :     {2}
  162.                 SystemClick(myEvent, whichWindow);
  163.             inContent :          {3}
  164.                 doContent(myEvent, whichWindow);
  165.             inDrag :             {4}
  166.                 doDrag(whichWindow, globalPt);
  167.             inGrow :         {5}
  168.                 doGrow(whichWindow, globalPt, False);
  169.             inGoAway :         {6}
  170.                 IF TrackGoAway(whichWindow, globalPt) THEN
  171.                     HideWindow(whichWindow);
  172.             inZoomIn, InZoomOut :        {7, 8}
  173.                 BEGIN
  174.                     IF TrackBox(whichWindow, globalPt, whereIsIt) THEN
  175.                         BEGIN
  176.                             GetPort(OldPort);
  177.                             SetPort(whichWindow); {safety device}
  178.                             EraseRect(whichWindow^.portRect);
  179.                             ZoomWindow(whichWindow, whereIsIt, True);
  180.                             doGrow(whichWindow, globalPt, True);
  181.                             SetPort(OldPort);
  182.                         END;
  183.                 END;
  184.             OTHERWISE
  185.                 BEGIN
  186.                 END;
  187.         END; {of whereIsIt}
  188.     END;
  189.  
  190.     PROCEDURE doKeyDowns (myEvent : EventRecord);
  191.         VAR
  192.             ch : char;
  193.             charCode : longInt;
  194.             keyCode : longInt;
  195.     BEGIN
  196.         charCode := BitAnd(myEvent.Message, charCodeMask);  {strip off key code}
  197.         keyCode := BitShift(BitAnd(myEvent.Message, keyCodeMask), -8); {strip off char}
  198.         ch := Chr(charCode);  {get keyboard char}
  199.         IF BitAnd(myEvent.Modifiers, CmdKey) = CmdKey THEN
  200.             doMenuBar(MenuKey(ch))  { do menu command key}
  201.         ELSE
  202.             BEGIN  { do keystroke }
  203.                 ParamText('No typing supported…', '', '', '');
  204.                 itemhit := CautionAlert(AlertDialog, NIL);
  205.             END;  { of do key stroke }
  206.     END;  { of proc}
  207.  
  208.     PROCEDURE doUpdates (myEvent : EventRecord);
  209.         VAR
  210.             UpdateWindow : WindowPtr;
  211.             TempPort : GrafPtr;
  212.     BEGIN
  213.         UpdateWindow := WindowPtr(myEvent.message);
  214.         IF UpdateWindow = PlotWindow THEN
  215.             BEGIN
  216.                 GetPort(TempPort); {save port}
  217.                 SetPort(UpdateWindow);
  218.                 BeginUpDate(UpdateWindow);
  219.                 BackColor(whiteColor);
  220.                 EraseRect(UpdateWindow^.portRect);
  221.                 ClipRect(UpdateWindow^.portRect);
  222.                 DrawPicture(DrawingPic, PicRect);
  223.                 DrawGrowIcon(UpdateWindow);
  224.                 EndUpDate(UpdateWindow);
  225.                 SetPort(TempPort);    {restore port}
  226.             END;
  227.     END; {of proc}
  228.  
  229.     PROCEDURE doActivates (myEvent : EventRecord);
  230.         VAR
  231.             TargetWindow : WindowPtr;
  232.             TargetPeek : WindowPeek;
  233.     BEGIN
  234.         TargetWindow := WindowPtr(myEvent.message);
  235.         TargetPeek := windowPeek(TargetWindow);
  236.         SetPort(TargetWindow);
  237.         IF Odd(myEvent.modifiers) THEN
  238.             BEGIN {activate}
  239.                 IF TargetWindow = PlotWindow THEN
  240.                     BEGIN
  241.                         DisableItem(myMenus[EditM], eUndo);
  242.                         DisableItem(myMenus[EditM], eCut);
  243.                         DisableItem(myMenus[EditM], eCopy);
  244.                         DisableItem(myMenus[EditM], ePaste);
  245.                         DisableItem(myMenus[EditM], eClear);
  246.                         DrawGrowIcon(TargetWindow);
  247.                     END
  248.             END  { of activate loop}
  249.         ELSE
  250.             BEGIN {deactivate}
  251.                 IF TargetWindow = PlotWindow THEN
  252.                     BEGIN
  253.                         EnableItem(myMenus[EditM], eUndo);
  254.                         EnableItem(myMenus[EditM], eCut);
  255.                         EnableItem(myMenus[EditM], eCopy);
  256.                         EnableItem(myMenus[EditM], ePaste);
  257.                         EnableItem(myMenus[EditM], eClear);
  258.                         DrawGrowIcon(TargetWindow);
  259.                     END; { of my window activation}
  260.             END; {of deactivate loop}
  261.     END; {of proc}
  262.  
  263.     PROCEDURE doMulti (myEvent : EventRecord);
  264.  
  265.         CONST
  266.             MouseMovedEvt = $FA;
  267.         VAR
  268.             HiByte : byte;
  269.             bit0 : LongInt;
  270.             sysresult : boolean;
  271.             ResumeWindow : WindowPtr;
  272.             ResumePeek : WindowPeek;
  273.             SuspendWindow : WindowPtr;
  274.             SuspendPeek : WindowPeek;
  275.             MouseMove : LongAndByte;
  276.     BEGIN
  277.         bit0 := 31; {convert 68000 to toolbox}
  278.     {check for mouse moved event}
  279.         MouseMove.longView := myEvent.message;
  280.         HiByte := Byte(MouseMove.byteView.byte0);
  281.         IF HiByte = MouseMovedEvt THEN
  282.             BEGIN {Handle mouse moved event}
  283.             END;
  284.     {check for resume event }
  285.         IF Odd(myEvent.message) THEN
  286.             BEGIN    {resume}
  287.          {activate Event}
  288.                 ResumeWindow := FrontWindow;
  289.                 IF ResumeWindow = PlotWindow THEN
  290.                     BEGIN
  291.                         SetPort(ResumeWindow);
  292.                         InvalRect(ResumeWindow^.portRect); { force update event}
  293.                         DisableItem(myMenus[EditM], eUndo);
  294.                         DisableItem(myMenus[EditM], eCut);
  295.                         DisableItem(myMenus[EditM], eCopy);
  296.                         DisableItem(myMenus[EditM], ePaste);
  297.                         DisableItem(myMenus[EditM], eClear);
  298.                         DrawGrowIcon(ResumeWindow);
  299.                     END;
  300.                 IF FrontWindow <> NIL THEN
  301.                     BEGIN {DA check}
  302.                         ResumePeek := WindowPeek(FrontWindow);
  303.                         IF ResumePeek^.windowKind < 0 THEN {DA}
  304.                             BEGIN
  305.                                 myEvent.what := activateEvt;
  306.                                 BitSet(@myEvent.modifiers, bit0);
  307.                                 sysresult := SystemEvent(myEvent);
  308.                             END; {da}
  309.                     END; {DA check}
  310.          { end of activate Event}
  311.             END {of resume}
  312.         ELSE
  313.             BEGIN    {suspend}
  314.         {de-activate Event}
  315.                 SuspendWindow := FrontWindow;
  316.                 IF SuspendWindow = PlotWindow THEN
  317.                     BEGIN {plotwindow}
  318.                         SetPort(SuspendWindow);
  319.                         InvalRect(SuspendWindow^.portRect); {force update}
  320.                         EnableItem(myMenus[EditM], eUndo);
  321.                         EnableItem(myMenus[EditM], eCut);
  322.                         EnableItem(myMenus[EditM], eCopy);
  323.                         EnableItem(myMenus[EditM], ePaste);
  324.                         EnableItem(myMenus[EditM], eClear);
  325.                         DrawGrowIcon(SuspendWindow);
  326.                     END; {plotwindow}
  327.                 IF FrontWindow <> NIL THEN
  328.                     BEGIN {DA check}
  329.                         SuspendPeek := WindowPeek(FrontWindow);
  330.                         IF SuspendPeek^.windowKind < 0 THEN
  331.                             BEGIN {da}
  332.                                 myEvent.what := activateEvt;
  333.                                 BitClr(@myEvent.modifiers, bit0);
  334.                                 sysresult := SystemEvent(myEvent);
  335.                             END; {da}
  336.                     END; {DA check}
  337.          { end of de-activate Event}
  338.             END; {suspend}
  339.     END; {of proc}
  340.  
  341.     PROCEDURE MainEventLoop;
  342.         CONST
  343.             MultiFinderEvt = 15;
  344.         VAR
  345.             sleep : LongInt;
  346.             Event : EventRecord;
  347.             DoIt : Boolean;
  348.     BEGIN
  349.         sleep := 10;
  350.         REPEAT
  351.             IF WNE THEN
  352.                 DoIt := WaitNextEvent(EveryEvent, Event, sleep, NIL) {no mouse tracking}
  353.             ELSE
  354.                 BEGIN
  355.                     SystemTask;
  356.                     DoIt := GetNextEvent(EveryEvent, Event);
  357.                 END;
  358.             IF DoIt THEN
  359.                 CASE Event.what OF
  360.                     mouseDown : 
  361.                         doMouse(Event);
  362.                     KeyDown, Autokey : 
  363.                         doKeyDowns(Event);
  364.                     updateEvt : 
  365.                         doUpdates(Event);
  366.                     activateEvt : 
  367.                         doActivates(Event);
  368.                     MultiFinderEvt : 
  369.                         doMulti(Event);
  370.                     OTHERWISE
  371.                         BEGIN
  372.                         END;
  373.                 END;    {of event case}
  374.         UNTIL Finished;     {end program}
  375.     END;
  376.  
  377. { Main Program}
  378. BEGIN
  379.     InitMac;
  380.     InitRects;
  381.     InitMyWindow;
  382.     InitMyMenus;
  383.     MainEventLoop;
  384. END.